from fastapi import APIRouter

from models.school import Student

school = APIRouter()


@school.get("/", description='添加和批量新增')
async def save():
    # 单条新增
    create_student_object = await Student.create(name="张无忌", pwd=123, sno=2009, clazzs_id=1)

    # 批量新增
    create_student_object_list = await Student.bulk_create(
        [Student(name="批量新增名称" + str(i), pwd=123, sno=2009 + i, clazzs_id=1) for i in range(3)]
    )
    return {'status': 'ok'}


@school.delete('/', description='删除方法')
async def de():
    dele_num = await  Student.filter(id=1).delete()
    return {'status': 'ok', 'num': dele_num}


@school.put("/edit")
async def edit():
    up_num = await  Student.filter(id=3).update(name='苏东坡')
    stulist = await  Student.all()
    for stu in stulist:
        stu.name += "@"
    return {'num': up_num, 'status': 'ok'}
